package com.city.parking;

import com.city.parking.provider.ParkingProviderHelper;
import com.city.parking.provider.ParkingRecord;
import com.city.parking.util.Utils;
import com.umeng.socialize.controller.UMServiceFactory;
import com.umeng.socialize.controller.UMSocialService;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.weixin.controller.UMWXHandler;

import android.app.Activity;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class ParkingDetailActivity extends FragmentActivity implements OnClickListener {
	private ParkingItem mParking = null;
	private ParkingRecord mParkingRecord = null;
	private String name, addr;
	private double lat, lng;
	private TextView mark, share;
	private UMSocialService mController;
	private TextView mChargeMode, mType, mIdle, mIndoorMap;
	private LinearLayout mIndoorMapLL;
	private Button mOrder;
	private Activity activiy;
	private ImageView mParkPic;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_parking_detail);
		TextView titleTv = (TextView) findViewById(R.id.nav_title);
	    mParking = getIntent().getParcelableExtra("park");
	    titleTv.setText(mParking.name);

        ImageView back = (ImageView) findViewById(R.id.nav_ico_back);
        back.setOnClickListener(this);

        ImageView more = (ImageView) findViewById(R.id.nav_ico_more);
        more.setVisibility(View.GONE);
        
        mark = (TextView) findViewById(R.id.parking_mark);
        mark.setOnClickListener(this);
        share = (TextView) findViewById(R.id.parking_share);
        share.setOnClickListener(this);

        mChargeMode = (TextView) findViewById(R.id.charge_mode);
        mType = (TextView) findViewById(R.id.parking_type);
        mIdle = (TextView) findViewById(R.id.parking_idle);
        mIndoorMap = (TextView) findViewById(R.id.indoor_map);
        mOrder = (Button) findViewById(R.id.parking_reserve);
		mParkPic = (ImageView) findViewById(R.id.parking_pic);

		long idx = Math.abs(System.currentTimeMillis()) % 4;
		switch ((int)idx) {
			case 0:
				mParkPic.setImageResource(R.drawable.p0);
				break;
			case 1:
				mParkPic.setImageResource(R.drawable.p1);
				break;
			case 2:
				mParkPic.setImageResource(R.drawable.p2);
				break;
			default:
				mParkPic.setImageResource(R.drawable.p3);
				break;
		}
		Utils.log("cgm parking id " + mParking.id + ", pic idx = " + idx);
        
        String charge_text, idle_text, type_text, indoor_text;
        if (mParking.id <= 0) {
        	mOrder.setEnabled(false);
        	charge_text = "未知";
        	idle_text = "未知";
        	type_text = "普通停车场";
        	indoor_text = "无";
        } else {
            if (mParking.isFree > 0)
            	charge_text = "免费";        	
            else
            	charge_text = mParking.charge + "元/小时";
        	idle_text = mParking.portLeftCount + " / " +mParking.portCount;
        	type_text = "合作停车场";
        	indoor_text = "点击进入";
        	mIndoorMapLL = (LinearLayout) findViewById(R.id.indoor_map_ll);
        	mIndoorMapLL.setOnClickListener(this);
        	mIndoorMap.setTextColor(Color.RED);
        }
        
    	mIndoorMapLL = (LinearLayout) findViewById(R.id.indoor_map_ll);
    	mIndoorMapLL.setOnClickListener(this);
    	mIndoorMap.setTextColor(Color.RED);
        
        mIdle.setText(idle_text);
        mChargeMode.setText(charge_text);
        mType.setText(type_text);
        mIndoorMap.setText(indoor_text);

        mParkingRecord = ParkingProviderHelper.getParkingByName(this, mParking.name);
        if (mParkingRecord != null) {
        	setMarkedParking();
        }
        
		mController = UMServiceFactory.getUMSocialService("com.umeng.share");
		// 设置分享内容
		mController.setShareContent("友盟社会化组件（SDK）让移动应用快速整合社交分享功能，http://www.umeng.com/social");
		// 设置分享图片, 参数2为图片的url地址
		mController.setShareMedia(new UMImage(this, "http://www.baidu.com/img/bdlogo.png"));
		
		String appID = "wx967daebe835fbeac";
		String appSecret = "5fa9e68ca3970e87a1f83e563c8dcbce";
		// 添加微信平台
		UMWXHandler wxHandler = new UMWXHandler(this, appID, appSecret);
		wxHandler.addToSocialSDK();
		// 添加微信朋友圈
		UMWXHandler wxCircleHandler = new UMWXHandler(this,appID,appSecret);
		wxCircleHandler.setToCircle(true);
		wxCircleHandler.addToSocialSDK();
		
		activiy = this;

//		if (!Utils.isEmptyText(mParking.mapAddr) && ParkingApp.get().mIndoorMapEnginee != null) {

		if (ParkingApp.get().mIndoorMapEnginee != null) {
			ParkingApp.get().mIndoorMapEnginee.init(this);
		}
    }
    
    private void setMarkedParking() {
    	Drawable drawable= getResources().getDrawable(R.drawable.parking_marked);
    	drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
    	mark.setCompoundDrawables(drawable, null, null, null);
    }


    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.nav_ico_back:
            finish();
            break;
        case R.id.parking_mark:
        	if (mParkingRecord == null) {
        		ParkingRecord pr = new ParkingRecord(lng, lat, addr, name);
        		int ret = ParkingProviderHelper.addParking(this, pr);
        		if (ret >= 0) {
        			Toast.makeText(this, "收藏停车场成功", Toast.LENGTH_SHORT).show();
        			setMarkedParking();
        		}
        	}
            break;
        case R.id.parking_share:
        	mController.openShare(this, false);
        	break;

        case R.id.indoor_map_ll:
			if (ParkingApp.get().mIndoorMapEnginee != null) {
				ParkingApp.get().mIndoorMapEnginee.show(mParking, this);
			}
        	break;

        default:
            break;
        }
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onPause() {
    	ParkingApp.get().hideProgressBar();
        super.onPause();
    }
}
